// 从后台获取的动态路由   
import { getnavbarList,isUpdateNavlist } from "@/api/head"
import router from "@/router"

const state={
    //导航栏路由
    navbarList:[]
}
const actions={
    getnavbarList(context){
        if(window.sessionStorage.getItem('navbarList')){
            let navbarList= JSON.parse( window.sessionStorage.getItem('navbarList'))
            //向后台查询导航栏有没有变化
            isUpdateNavlist().then(res=>{
               if(res.code===210) {
                navbarList=res.data              
               }
               context.commit('setnavbarList',navbarList)
            })
        }else{
            getnavbarList().then(list=>{
                context.commit('setnavbarList',list)
            })}
    }
}
const mutations={
    setnavbarList(state,list){     
      state.navbarList=list
        window.sessionStorage.setItem('navbarList',JSON.stringify(state.navbarList))
        state.navbarList.forEach(nav => {
            router.addRoute({     
                path:nav.path,
                component:(resolve) => require([`@/views/`+nav.component], resolve),
                meta:nav.meta
            })
          
        });
        console.log(router.getRoutes())
    }
}
const getters={}






export default {
    namespaced: true,
    state,
    actions,
    getters,
    mutations
}